/**
 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 * 
 * 项目名：	cards-web
 * 文件名：	WxAppSessionCache.java
 * 模块说明：	
 * 修改历史：
 * 2017年4月13日 - Debenson - 创建。
 */
package com.alex.wechat.app.config;

import java.io.Serializable;
import java.util.UUID;

import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang3.StringUtils;

import com.alex.insistence.rest.user.RsAppUser;

import me.chanjar.weixin.common.util.JsonMapper;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * 用来缓存小程序的第三方session
 * 
 * @author Debenson
 * @since 0.1
 */
public final class ThirdSessionCache {

  private static Cache getDefaultCache() {
    Cache cache = CacheManager.getInstance().getCache("dictCache");
    return cache;
  }

  /**
   * 生成第三方session
   * 
   * @param appId
   * @param openId
   * @param userInfo
   *          用户信息
   * @param wxSessionKey
   * @param expireInSeconds
   * @return 第三方session信息。
   */
  public static ThirdSession create(String appId, String openId, RsAppUser userInfo,
      String wxSessionKey, int expireInSeconds) {
    final String thirdSessionKey = UUID.randomUUID().toString().replaceAll("-", "");

    ThirdSession thirdSession = new ThirdSession();
    thirdSession.setAppId(appId);
    thirdSession.setOpenId(openId);
    thirdSession.setUnionId(userInfo == null ? null : userInfo.getUnionId());
    thirdSession.setUserInfo(userInfo);
    thirdSession.setWxSessionKey(wxSessionKey);
    thirdSession.setThirdSessionKey(thirdSessionKey);
    Element element = new Element(thirdSessionKey, JsonMapper.defaultMapper().toJson(thirdSession));
    element.setTimeToLive(expireInSeconds);
    getDefaultCache().put(element);

    return thirdSession;
  }

  /**
   * 取得第三方session
   * 
   * @param thirdSessionKey
   *          {@link #create(String, String, int)}
   * @return
   */
  public static ThirdSession get(String thirdSessionKey) {
    Element element = getDefaultCache().get(thirdSessionKey);
    if (element == null) {
      return null;
    }

    String thirdSessionValue = (String) element.getValue();
    if (StringUtils.isBlank(thirdSessionValue)) {
      return null;
    } else {
      return JsonMapper.defaultMapper().fromJson(thirdSessionValue, ThirdSession.class);
    }
  }

  /**
   * @author Debenson
   * @since 0.1
   */
  @XmlRootElement
  public static class ThirdSession implements Serializable {
    private static final long serialVersionUID = -6686524656254782571L;

    private String wxSessionKey;
    private String openId;
    private String unionId;
    private String appId;
    private String thirdSessionKey;
    private RsAppUser userInfo;

    /** 用户唯一标识 */
    public String getOpenId() {
      return openId;
    }

    public void setOpenId(String openId) {
      this.openId = openId;
    }

    /** 微信的session key */
    public String getWxSessionKey() {
      return wxSessionKey;
    }

    public void setWxSessionKey(String wxSessionKey) {
      this.wxSessionKey = wxSessionKey;
    }

    /** 小程序账户appid */
    public String getAppId() {
      return appId;
    }

    public void setAppId(String appId) {
      this.appId = appId;
    }

    /** 第三方session key */
    public String getThirdSessionKey() {
      return thirdSessionKey;
    }

    public void setThirdSessionKey(String thirdSessionKey) {
      this.thirdSessionKey = thirdSessionKey;
    }

    public String getUnionId() {
      return unionId;
    }

    public void setUnionId(String unionId) {
      this.unionId = unionId;
    }

    /** 用户信息 */
    public RsAppUser getUserInfo() {
      return userInfo;
    }

    public void setUserInfo(RsAppUser userInfo) {
      this.userInfo = userInfo;
    }

  }

}
